package cn.edu.scau.dao;

import cn.edu.scau.entity.Care;

import java.util.List;

import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.web.bind.annotation.ResponseBody;

@Mapper
@CacheNamespace
@ResponseBody
public interface CareMapper {
	/**
	 * 根据主键删除
	 * @param customerid
	 * @return
	 */
    @Delete({
        "delete from care",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer customerid);

    /**
     * 新增照顾信息，不考虑null
     * @param record
     * @return
     */
    @Insert({
        "insert into care (customerid, nurseid)",
        "values (#{customerid,jdbcType=INTEGER}, #{nurseid,jdbcType=INTEGER})"
    })
    int insert(Care record);

    /**
     * 新增照顾信息,考虑null。若为空，不向数据库插入数据
     * @param record
     * @return
     */
    @InsertProvider(type=CareSqlProvider.class, method="insertSelective")
    int insertSelective(Care record);

    /**
     * 根据主键查找
     * @param customerid
     * @return
     */
    @Select({
        "select",
        "customerID, nurseID",
        "from care",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="customerID", property="customerid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="nurseID", property="nurseid", jdbcType=JdbcType.INTEGER)
    })
    Care selectByPrimaryKeyNoNameNoName(Integer customerid);
    
    /**
     * 根据主键查询，会查询客户名和护士名
     * @param customerid
     * @return
     */
    @Select({
        "select",
        "customerID, customerName, nurseID, nurseName",
        "from care natural join nurse natural join customer",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="customerID", property="customerid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customerName", property="customername", jdbcType=JdbcType.VARCHAR),
        @Result(column="nurseID", property="nurseid", jdbcType=JdbcType.INTEGER),
        @Result(column="nurseName", property="nursename", jdbcType=JdbcType.VARCHAR)
    })
    Care selectByPrimaryKey(Integer customerid);

    /**
     * 查找所有，不查找名字
     * @return
     */
    @Select({
        "select",
        "customerID, nurseID",
        "from care"
    })
    List<Care> selectAllNoName();
    
    /**
     * 查找所有，会查找名字
     * @return
     */
    @Select({
        "select",
        "customerID, customerName, nurseID, nurseName",
        "from care natural join nurse natural join customer",
        "order by customerID asc"
    })
    List<Care> selectAll();
    
    /**
     *  根据护士id求出对应的客户id
     * @param nurseid
     * @return
     */
    @Select({
        "select",
        "customerID",
        "from care",
        "where nurseID = #{nurseid,jdbcType=INTEGER}"
    })

    List<Integer> selectByNurseId(Integer nurseid);
    
    /**
     * 根据护士编号查找照顾信息
     * @param customerid
     * @return
     */
    @Select({
        "select",
        "customerID, customerName, nurseID, nurseName",
        "from care natural join nurse natural join customer",
        "where nurseID = #{nurseid,jdbcType=INTEGER}",
        "order by customerID asc"
    })
    @Results({
        @Result(column="customerID", property="customerid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customerName", property="customername", jdbcType=JdbcType.VARCHAR),
        @Result(column="nurseID", property="nurseid", jdbcType=JdbcType.INTEGER),
        @Result(column="nurseName", property="nursename", jdbcType=JdbcType.VARCHAR)
    })
    List<Care> selectByNurseID(Integer nurseid);
    
    /**
     * 修改照顾信息表，,考虑null。若为空，不会更改数据库数据
     * @param record
     * @return
     */
    @UpdateProvider(type=CareSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Care record);

    /**
     * 修改照顾信息表，不考虑null。若为空，会将对应属性修改为空
     * @param record
     * @return
     */
    @Update({
        "update care",
        "set nurseID = #{nurseid,jdbcType=INTEGER}",
        "where customerID = #{customerid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Care record);
    
    /**
     * 根据客户编号、客户名、护士编号、护士名组合查询
     * @param customerid
     * @param customername
     * @param nurseid
     * @param nursename
     * @return
     */
    @SelectProvider(type = CareSqlProvider.class,method = "find")
    List<Care> find(@Param("customerid") Integer customerid,@Param("customername") String customername,@Param("nurseid") Integer nurseid,@Param("nursename") String nursename);
    
    /**
     * 根据客户编号、客户名、护士编号组合查询，护士编号已经确定
     * @param customerid
     * @param customername
     * @param nurseid
     * @param nursename
     * @return
     */
    @SelectProvider(type = CareSqlProvider.class,method = "findWithNurseID")
    List<Care> findWithNurseID(@Param("customerid") Integer customerid,@Param("customername") String customername,@Param("nurseid") Integer nurseid,@Param("nursename") String nursename);
}